package model

import (
	"time"
	"user_srv/basic/config"
)

// 灵魂问答题目表
type XqSoulQuestions struct {
	Id           int64     `gorm:"column:id;type:bigint(20);comment:题目ID;primaryKey;not null;" json:"id"`
	Question     string    `gorm:"column:question;type:varchar(500);comment:题目内容;not null;" json:"question"`
	Category     string    `gorm:"column:category;type:varchar(50);comment:题目分类;not null;" json:"category"`
	QuestionType int8      `gorm:"column:question_type;type:tinyint(4);comment:题目类型：1-单选题，2-多选题;not null;default:1;" json:"question_type"`
	Options      string    `gorm:"column:options;type:json;comment:选项内容(JSON格式);" json:"options"`
	Weight       float64   `gorm:"column:weight;type:decimal(3,2);comment:题目权重;not null;default:1.00;" json:"weight"`
	SortOrder    int32     `gorm:"column:sort_order;type:int(11);comment:排序权重;not null;default:0;" json:"sort_order"`
	IsActive     int8      `gorm:"column:is_active;type:tinyint(1);comment:是否启用;not null;default:1;" json:"is_active"`
	CreatedAt    time.Time `gorm:"column:created_at;type:timestamp;not null;default:CURRENT_TIMESTAMP;" json:"created_at"`
	UpdatedAt    time.Time `gorm:"column:updated_at;type:timestamp;not null;default:CURRENT_TIMESTAMP;" json:"updated_at"`
}

func (x *XqSoulQuestions) TableName() string {
	return "xq_soul_questions"
}

func (x *XqSoulQuestions) Create() error {
	return config.DB.Create(&x).Error
}

func (x *XqSoulQuestions) Delete(where XqSoulQuestions) error {
	return config.DB.Where(where).Delete(&x).Error
}

func (x *XqSoulQuestions) Update(where XqSoulQuestions) error {
	return config.DB.Where(where).Updates(&x).Error
}

func (x *XqSoulQuestions) List(where XqSoulQuestions) ([]XqSoulQuestions, error) {
	var questions []XqSoulQuestions
	err := config.DB.Where(where).Find(&questions).Error
	return questions, err
}

func (x *XqSoulQuestions) GetById(id int64) (*XqSoulQuestions, error) {
	var question XqSoulQuestions
	err := config.DB.Where("id = ? AND is_active = 1", id).First(&question).Error
	return &question, err
}

func (x *XqSoulQuestions) ListByCategory(category string, limit int) ([]XqSoulQuestions, error) {
	var questions []XqSoulQuestions
	query := config.DB.Where("is_active = 1")
	if category != "" {
		query = query.Where("category = ?", category)
	}
	err := query.Order("sort_order ASC, id ASC").Limit(limit).Find(&questions).Error
	return questions, err
}
